package com.cwd.arm.domain.repository.impl;

import com.cwd.arm.domain.entity.ConfirmIncomeEntity;
import com.cwd.arm.domain.entity.ProjectEntity;
import com.cwd.arm.domain.repository.ConfirmIncomeRepository;
import com.cwd.arm.domain.repository.ProjectRepository;
import com.cwd.arm.infrastructure.dao.ConfirmIncomeDao;
import com.cwd.arm.infrastructure.po.ConfirmIncomePo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * Description:确认收入
 * Created on 2021年11月16日
 *
 * @author yan
 */
@Repository
public class ConfirmIncomeRepositoryImpl implements ConfirmIncomeRepository {

    @Autowired
    private ConfirmIncomeDao dao;

    @Autowired
    private ProjectRepository projectRepository;

    @Override
    public void save(ConfirmIncomeEntity confirmIncomeEntity) {
        ProjectEntity projectEntity = projectRepository.buildProjectEntity(confirmIncomeEntity.getProjectId());
//        if (!confirmIncomeEntity.canAdd(projectEntity.getContractAmount(), confirmIncomeEntity.getConfirmAmount())) {
//            throw new ArmException(ErrorEnum.VAILD_ERROR,"确认金额与进度不匹配");
//        }
        dao.save(convertToConfirmIncomePo(confirmIncomeEntity));
        projectRepository.addConfirmIncome(projectEntity, confirmIncomeEntity);
    }

    @Override
    public void update(ConfirmIncomeEntity confirmIncomeEntity) {
        ConfirmIncomeEntity oldConfirmIncomeEntity = buildConfirmIncomeEntity(confirmIncomeEntity.getId());
        ProjectEntity projectEntity = projectRepository.buildProjectEntity(oldConfirmIncomeEntity.getProjectId());
//        if (!confirmIncomeEntity.canUpdate(projectEntity.getContractAmount(),
//                confirmIncomeEntity.getConfirmAmount(),oldConfirmIncomeEntity.getConfirmAmount())) {
//            throw new ArmException(ErrorEnum.VAILD_ERROR,"确认金额与进度不匹配");
//        }
        dao.updateById(convertToConfirmIncomePo(confirmIncomeEntity));
        projectRepository.updateConfirmIncome(projectEntity, oldConfirmIncomeEntity,confirmIncomeEntity);

    }

    @Override
    public void del(Long id) {
        ConfirmIncomeEntity confirmIncomeEntity = buildConfirmIncomeEntity(id);
        dao.removeById(id);
        ProjectEntity projectEntity = projectRepository.buildProjectEntity(confirmIncomeEntity.getProjectId());
        projectRepository.delConfirmIncome(projectEntity,confirmIncomeEntity);
    }

    private ConfirmIncomeEntity buildConfirmIncomeEntity(Long id){
        return convertToConfirmIncomeEntity(dao.getById(id));
    }

    private ConfirmIncomePo convertToConfirmIncomePo(ConfirmIncomeEntity entity) {
        ConfirmIncomePo ConfirmIncomePo = new ConfirmIncomePo();
        BeanUtils.copyProperties(entity, ConfirmIncomePo);
        return ConfirmIncomePo;
    }

    private ConfirmIncomeEntity convertToConfirmIncomeEntity(ConfirmIncomePo po) {
        ConfirmIncomeEntity confirmIncomeEntity = new ConfirmIncomeEntity();
        BeanUtils.copyProperties(po, confirmIncomeEntity);
        return confirmIncomeEntity;
    }

}
